package personal.xjl.plusdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import personal.xjl.plusdemo.domain.Admin;
import personal.xjl.plusdemo.service.AdminService;

import java.util.List;

@Controller
@RequestMapping("Admin")
public class AdminController {
    @Autowired
    AdminService AdminService;
    @RequestMapping("list.do")
    public String list(Model model) {
        List<Admin> adminList = AdminService.list();
        model.addAttribute("adminList", adminList);
        return "listAdmins";
    }
    @RequestMapping("delete.do")
    //  @ResponseBody
    public String delete(@RequestParam("id") int id, Model model) {
        AdminService.removeById(id);
        return "redirect:/Admin/list.do";
    }
    @RequestMapping("page.do")
    public String page(@RequestParam("page") String page) {
        return page;
    }
    @RequestMapping("add.do")
    public String add(Admin admin) {
        System.out.println(admin);
        AdminService.save(admin);
        return "redirect:/Admin/list.do";
    }
    //准备修改学生信息
    @RequestMapping("toEdit.do")
    public String edit(@RequestParam("id") int id, Model model) {
        Admin oldAdmin=AdminService.getById(id);
        model.addAttribute("admin",oldAdmin);
        return "editAdmin";
    }
    //修改学生信息
    @RequestMapping("edit.do")
    public String edit(Admin admin) {
        AdminService.saveOrUpdate(admin);
        return "redirect:/Admin/list.do";
    }
    /*    //登录,如果登录成功，跳转到list.do，显示学生数据；如果登录失败，回到login页面
        @RequestMapping("login.do")
        public String login(HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password) {
            if(AdminService.login(username,password)) {
                //发放登录的令牌，username
                session.setAttribute("username",username);
                return "redirect:/Admin/list.do";
            }
            else
                return "login";
        }*/
/*    @RequestMapping("loginNew.do")
    public String loginNew() {
        return "login";
    }*/
}
